﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-17 22:40
    /// 描 述：服务类
    /// </summary>
    public class StoreOrderDetailService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<StoreOrderDetailEntity>> GetList(StoreOrderDetailListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<StoreOrderDetailEntity>> GetPageList(StoreOrderDetailListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<StoreOrderDetailEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<StoreOrderDetailEntity>(id);
        }

        public async Task<List<OrderDetailDto>> GetPageDetailList(OrderDetailListParam param, Pagination pagination)
        {
            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.Id,a.goods_id,a.order_id,a.pw_id,a.shop_id,a.goods_price,b.order_sn,b.shipping_user,b.ship_mobile,b.ordernumber,b.product_cnt
                            ,b.create_time,b.order_money,c.goods_name,c.goods_image,d.shop_name FROM storeOrderDetail  a 
                            JOIN storeOrderMaster b ON a.order_id=b.Id
                            JOIN storeGoods c on a.goods_id=c.Id 
                            JOIN store d on a.shop_id=d.Id 
                             where 1=1 ");

            if (!param.IsEmpty())
            {
                if (!param.order_sn.IsNullOrZero())
                {
                    strSql.Append(" and b.order_sn like @order_sn ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@order_sn", '%' + param.order_sn + '%'));
                }
                if (!param.shop_name.IsEmpty())
                {
                    strSql.Append(" and d.shop_name like @shop_name ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@shop_name", '%' + param.shop_name + '%'));
                }
                if (!param.goods_name.IsEmpty())
                {
                    strSql.Append(" and c.goods_name like @goods_name ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@goods_name", '%' + param.goods_name + '%'));
                }
                if (!param.StartTime.IsEmpty())
                {
                    strSql.Append(" AND b.create_time>=@StartTime ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@StartTime", param.StartTime));
                }
                if (!param.EndTime.IsEmpty())
                {
                    param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    strSql.Append(" AND b.create_time<=@EndTime ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@EndTime", param.EndTime));
                }
                if (!param.shopids.IsEmpty())
                {
                    if (param.shopids != "-1")
                    {
                        var shoparr = TextHelper.SplitToArray<long>(param.shopids, ',');
                        if (shoparr.Count() > 0)
                        {
                            strSql.Append(" AND d.Id in(@shoparr) ");
                            parameter.Add(DbParameterExtension.CreateDbParameter("@shoparr", param.shopids));
                        }
                    }
                }
            }
            IEnumerable<OrderDetailDto> list = await this.BaseRepository().FindList<OrderDetailDto>(strSql.ToString(), parameter.ToArray(), pagination);

           

            return list.ToList();
        }

        public async Task OrderDetailSaveFormJson(StoreOrderDetailEntity entity)
        {
            await this.BaseRepository().Update(entity);
        }

        public async Task<List<OrderDetailDto>> GetPageColourDetailList(OrderDetailListParam param, Pagination pagination)
        {
            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.Id,a.goods_id,a.order_id,a.productids,a.shop_id,a.colorid,b.order_sn,a.goods_cnt as product_cnt,
                            c.goods_name,e.attr_values as goods_image,e.attr_name as color,d.shop_name FROM storeOrderDetail  a 
                            JOIN storeOrderMaster b ON a.order_id=b.Id
                            JOIN storeGoods c on a.goods_id=c.Id 
                            JOIN store d on a.shop_id=d.Id 
                            JOIN storeGoodsAttr e on a.colorid=e.Id 
                            where 1=1 ");

            if (!param.IsEmpty())
            {
                if (!param.order_sn.IsNullOrZero())
                {
                    strSql.Append(" and b.order_sn like @order_sn ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@order_sn", '%' + param.order_sn + '%'));
                }
                if (!param.orderid.IsNullOrZero())
                {
                    strSql.Append(" and a.order_id=@order_id ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@order_id", param.orderid));
                }
                if (!param.shop_name.IsEmpty())
                {
                    strSql.Append(" and d.shop_name like @shop_name ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@shop_name", '%' + param.shop_name + '%'));
                }
                if (!param.goods_name.IsEmpty())
                {
                    strSql.Append(" and c.goods_name like @goods_name ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@goods_name", '%' + param.goods_name + '%'));
                }
                
                if (!param.StartTime.IsEmpty())
                {
                    strSql.Append(" AND b.create_time>=@StartTime ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@StartTime", param.StartTime));
                }
                if (!param.EndTime.IsEmpty())
                {
                    param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    strSql.Append(" AND b.create_time<=@EndTime ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@EndTime", param.EndTime));
                }
                if (!param.shopids.IsEmpty())
                {
                    if (param.shopids != "-1")
                    {
                        var shoparr = TextHelper.SplitToArray<long>(param.shopids, ',');
                        if (shoparr.Count() > 0)
                        {
                            strSql.Append(" AND d.Id in(@shoparr) ");
                            parameter.Add(DbParameterExtension.CreateDbParameter("@shoparr", param.shopids));
                        }
                    }
                }
            }
            IEnumerable<OrderDetailDto> list = await this.BaseRepository().FindList<OrderDetailDto>(strSql.ToString(), parameter.ToArray(), pagination);

            foreach (var item in list)
            {
                //查询车架号
                if (!item.productids.IsEmpty())
                {
                    var pwids = TextHelper.SplitToArray<long>(item.productids, ',');

                    var pws = this.BaseRepository().FindList<StoreGoodsProductEntity>(t => pwids.Contains(t.Id.Value)).Result.ToList().Select(t => t.VIN);

                    if (pws.Count() > 0)
                    {
                        item.VIN = string.Join(',', pws);
                    }
                }
            }

            return list.ToList();
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(StoreOrderDetailEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<StoreOrderDetailEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<StoreOrderDetailEntity, bool>> ListFilter(StoreOrderDetailListParam param)
        {
            var expression = LinqExtensions.True<StoreOrderDetailEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
